home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / multiroots / fdjac.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-09  |  2.4 KB  |  97 lines

  1. /* multiroots/fdjac.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Brian Gough
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. #include <config.h>
  21. #include <gsl/gsl_multiroots.h>
  22.  
  23. int
  24. gsl_multiroot_fdjacobian (gsl_multiroot_function * F,
  25.                            const gsl_vector * x, const gsl_vector * f,
  26.                            double epsrel, gsl_matrix * jacobian)
  27. {
  28.   const size_t n = x->size;
  29.   const size_t m = f->size;
  30.   const size_t n1 = jacobian->size1;
  31.   const size_t n2 = jacobian->size2;
  32.  
  33.   if (m != n1 || n != n2)
  34.     {
  35.       GSL_ERROR ("function and jacobian are not conformant", GSL_EBADLEN);
  36.     }
  37.  
  38.   {
  39.     size_t i,j;
  40.     gsl_vector *x1, *f1;
  41.  
  42.     x1 = gsl_vector_alloc (n);
  43.  
  44.     if (x1 == 0)
  45.       {
  46.     GSL_ERROR ("failed to allocate space for x1 workspace", GSL_ENOMEM);
  47.       }
  48.  
  49.     f1 = gsl_vector_alloc (m);
  50.  
  51.     if (f1 == 0)
  52.       {
  53.     gsl_vector_free (x1);
  54.  
  55.     GSL_ERROR ("failed to allocate space for f1 workspace", GSL_ENOMEM);
  56.       }
  57.  
  58.     gsl_vector_memcpy (x1, x);    /* copy x into x1 */
  59.  
  60.     for (j = 0; j < n; j++)
  61.       {
  62.     double xj = gsl_vector_get (x, j);
  63.     double dx = epsrel * fabs (xj);
  64.  
  65.     if (dx == 0)
  66.       {
  67.         dx = epsrel;
  68.       }
  69.  
  70.     gsl_vector_set (x1, j, xj + dx);
  71.         
  72.         {
  73.           int status = GSL_MULTIROOT_FN_EVAL (F, x1, f1);
  74.  
  75.           if (status != GSL_SUCCESS) 
  76.             {
  77.               return GSL_EBADFUNC;
  78.             }
  79.         }
  80.  
  81.     gsl_vector_set (x1, j, xj);
  82.  
  83.     for (i = 0; i < m; i++)
  84.       {
  85.         double g1 = gsl_vector_get (f1, i);
  86.         double g0 = gsl_vector_get (f, i);
  87.         gsl_matrix_set (jacobian, i, j, (g1 - g0) / dx);
  88.       }
  89.       }
  90.  
  91.     gsl_vector_free (x1);
  92.     gsl_vector_free (f1);
  93.   }
  94.   
  95.   return GSL_SUCCESS;
  96. }
  97.